package com.data.JVM.chapter08;

import static java.lang.invoke.MethodHandles.lookup;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;

/**
 *
 *
 */
public class MethodHandleTest {
    static class ClassA{
        public void println(String g){
            System.out.println(g);

        }
    }

    public static void main(String[] args) throws Throwable {
        Object obj = System.currentTimeMillis()%2==0?System.out:new ClassA();

        getPrintlnMH(obj).invokeExact("icksahdf;oai");
    }

    private static MethodHandle getPrintlnMH(Object obj) throws NoSuchMethodException, IllegalAccessException {
        MethodType mt = MethodType.methodType(void.class,String.class);

        return lookup().findVirtual(obj.getClass(),"println",mt).bindTo(obj);
    }
}
